package xp2p.svc;

import java.util.Enumeration;

import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.document.Advertisement;

public abstract class ListenerBase<T extends Advertisement> implements DiscoveryListener
{
	private String docType;
	private String advClassType;	
	
	@Override
	public void discoveryEvent(DiscoveryEvent event)
	{		
		Enumeration<Advertisement> results = event.getSearchResults();
		process(results);		
	}
	
	@SuppressWarnings("unchecked")
	protected void process(Enumeration<Advertisement> results)
	{
		while(results.hasMoreElements())
		{
			Advertisement a = results.nextElement();
			String advType = a.getAdvType();
			if (advType.compareTo(getAdvClassType()) == 0)
			{
				process((T)a);
			}
		}
	}	
	
	protected abstract void process(T a);

	public String getDocType()
	{
		return docType;
	}

	public void setDocType(String docType)
	{
		this.docType = docType;
	}

	public String getAdvClassType()
	{
		return advClassType;
	}

	public void setAdvClassType(String advClassType)
	{
		this.advClassType = advClassType;
	}
}
